package com.tongtech.space.common.utils.desensitize;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 脱敏处理组装实现类
 *
 * @author jinjiayu
 * @since 2021/4/12
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DesensitizeWrapperImpl<T, R> extends AbstractDesensitizeWrapper<T, R> {
    public static final String NAME = "NAME";
    public static final String CARD_ID = "CARD_ID";
    public static final String MOBILE = "MOBILE";
    public static final String UNICODE = "UNICODE";
    public static final String ZIDUAN = "ZIDUAN";
    // TODO 更多脱敏规则待定...

    public DesensitizeWrapperImpl(T obj) {
        super(obj);
    }

    /**
     * 实现具体脱敏处理规则
     * TODO 待定
     * @param fieldValue
     * @param desensitizeType
     * @return String
     * @author jinjiayu
     */
    @Override
    public String doRule(String fieldValue, String desensitizeType) {
        switch (desensitizeType) {
            case NAME :
                fieldValue = desensitizeName(fieldValue);
                break;
            case CARD_ID :
                fieldValue = desensitizeCardId(fieldValue);
                break;
            case MOBILE :
                fieldValue = desensitizeMobile(fieldValue);
                break;
            case UNICODE :
                fieldValue = desensitizeUnicode(fieldValue);
                break;
            case ZIDUAN :
                fieldValue = desensitizeZiduan(fieldValue);
                break;
            // TODO 更多脱敏规则待定...

            default:
        }
        return fieldValue;
    }


    /**
     * 手机号脱敏
     * TODO 规则待定
     * @param mobile 手机号
     * @return String
     * @author jinjiayu
     */
    public static String desensitizeMobile(String mobile) {
        if(mobile ==null || mobile .length() < 2) {
            return mobile;
        }
        StringBuilder str = new StringBuilder();
        for(int j = 0;j < mobile.length() - 4 ;j++) {
            str.append("*");
        }
        return str+mobile.substring(mobile.length()-4);
    }

    /**
     * 统一信用代码脱敏
     * TODO 规则待定
     * @param unicode 统一信用代码
     * @return String
     * @author jinjiayu
     */
    public static String desensitizeUnicode(String unicode) {
        StringBuilder str = new StringBuilder();
        if(unicode ==null || unicode .length() < 2){
            return unicode;
        }else if(unicode .length() == 2){
            str = new StringBuilder("*" + unicode.substring(1, 2));
        }else if(unicode .length() == 3){
            str = new StringBuilder("**" + unicode.substring(2, 3));
        }else {
            for(int j = 0;j < unicode.length() - 2 ;j++) {
                str.append("*");
            }
            str.append(unicode.substring(unicode.length() - 2));
        }
        return str.toString();
    }

    /**
     * 统一信用代码脱敏
     * TODO 规则待定
     * @param unicode 统一信用代码
     * @return String
     * @author jinjiayu
     */
    public static String desensitizeZiduan(String unicode) {
        StringBuilder str = new StringBuilder();
        if(unicode ==null || unicode .length() < 2){
            return unicode;
        }else if(unicode .length() == 2){
            str = new StringBuilder("*" + unicode.substring(1, 2));
        }else if(unicode .length() == 3){
            str = new StringBuilder("**" + unicode.substring(2, 3));
        }else {
            for(int j = 0;j < unicode.length() - 2 ;j++) {
                str.append("*");
            }
            str.append(unicode.substring(unicode.length() - 2));
        }
        return str.toString();
    }

    /**
     * 身份证脱敏
     * TODO 规则待定
     * @param cardId 身份证
     * @return String
     * @author jinjiayu
     */
    public static String desensitizeCardId(String cardId) {
        if(cardId ==null || cardId .length() < 2) {
            return cardId;
        }
        StringBuilder str = new StringBuilder();
        for(int j = 0;j < cardId.length() - 4 ;j++) {
            str.append("*");
        }
        return str+cardId.substring(cardId.length()-4);
    }

    /**
     * 姓名脱敏
     * TODO 规则待定
     * @param name 姓名
     * @return String
     * @author jinjiayu
     */
    public static String desensitizeName(String name) {
        StringBuilder str = new StringBuilder();
        if(name ==null || name .length() < 2){
            return name;
        }else if(name .length() == 2){
            str = new StringBuilder("*" + name.substring(1, 2));
        }else if(name .length() == 3){
            str = new StringBuilder("**" + name.substring(2, 3));
        }else {
            for(int j = 0;j < name.length() - 2 ;j++) {
                str.append("*");
            }
            str.append(name.substring(name.length() - 2));
        }
        return str.toString();
    }

    // TODO 更多脱敏规则待定...



}
